* Create a record of any past interactions between AMI patients and a cardiologist

use "$savedata/masterdata.dta", replace

sort extract ami_date

keep extract pconsult finyear ami_date admidate*
rename admidate* ami_admidate*
compress

bys extract: gen x=_n
keep if x==1

save "$savedata/pats_and_cons.dta", replace


foreach year in 4 5 6 7 8{
	
	local next=`year'+1
	
use "$rawdata/ip_200`year'", clear

cap gen extract_hesid = encrypted_hesid


* Just keep those who've been treated in the connected set
merge m:1 extract using "$savedata/pats_and_cons.dta"
keep if _merge==3
rename _merge _merge_orig
drop pconsult ami*

* Merge in the pconsult for each of these episodes
local epiyear "0`year'0`next'" 
merge 1:1 epikey using "$consids/id_`epiyear'.dta"
drop if _merge!=3
assert _merge==3
drop _merge

gen admidate = mdy(admidate_month, admidate_day, admidate_year)

keep extract pconsult admidate admidate*

save "$savedata/temp200`year'.dta", replace
}

foreach year in 9{
	
	local next=`year'+1
	
use "$rawdata/ip_200`year'", clear

cap gen extract_hesid = encrypted_hesid

bys epikey: gen z=_n
keep if z==1

* Just keep those who've been treated in the connected set
merge m:1 extract using "$savedata/pats_and_cons.dta"
keep if _merge==3
rename _merge _merge_orig
drop pconsult ami*

* Merge in the pconsult for each of these episodes
local epiyear "0`year'`next'" 
merge 1:1 epikey using "$consids/id_`epiyear'.dta"
drop if _merge!=3
assert _merge==3
drop _merge

gen admidate = mdy(admidate_month, admidate_day, admidate_year)

keep extract pconsult admidate admidate*

save "$savedata/temp20`year'.dta", replace
}


foreach year in 10 11 12 13 14 15 {
	
use "$rawdata/ip_20`year'", clear

cap gen extract_hesid = encrypted_hesid

* Just keep those who've been treated in the connected set
merge m:1 extract using "$savedata/pats_and_cons.dta"
keep if _merge==3
rename _merge _merge_orig
drop ami*

cap gen admidate = mdy(admidate_month, admidate_day, admidate_year)

keep extract pconsult admidate admidate*

save "$savedata/temp20`year'.dta", replace
}

foreach year in 16 17{
	
use "$rawdata/ip_20`year'", clear

cap gen extract_hesid = encrypted_hesid

gen admidate_year = substr(admidate,1,4)
gen admidate_month = substr(admidate,6,2)
gen admidate_day = substr(admidate,9,2)

destring admidate_year, replace
destring admidate_month, replace
destring admidate_day, replace

drop admidate
gen admidate = mdy(admidate_month, admidate_day, admidate_year)

* Just keep those who've been treated in the connected set
merge m:1 extract using "$savedata/pats_and_cons.dta"
keep if _merge==3
rename _merge _merge_orig
drop ami*

cap gen admidate = mdy(admidate_month, admidate_day, admidate_year)

keep extract pconsult admidate admidate*

save "$savedata/temp20`year'.dta", replace
}


use "$savedata/temp2004.dta", clear

foreach year in 05 06 07 08 {
	append using "$savedata/temp20`year'.dta"
}

	
append using "$savedata/temp209.dta"
	
foreach year in	10 11 12 13 14 15 16 17{
append using "$savedata/temp20`year'.dta"
}

merge m:1 extract pconsult using "$savedata/pats_and_cons.dta"

gen diff = ami_date - admidate
gen match = 0
replace match = 1 if diff<-1

collapse (max) match, by(extract)

save "$savedata/no_past_interactions.dta", replace
